from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By

from common.Message import *
from common.Tools import Tools
from common.UiSelector import UiSelector
from page.general import General


class MainPage(General, metaclass=Tools):
    DOWNLOADING = 'downloading.jpg'
    SKIP = 'skip.jpg'
    CLOSE_CONTENT = 'close_content.jpg'
    CONNECT_CLOSE = 'connect_close.jpg'
    MAIN = 'main.jpg'
    NEED_UPDATE = 'need_update.jpg'
    PRESENT_BOX = 'present_box.jpg'
    NO_PRESENT = 'no_present.jpg'
    CONNECT_CONFIRM = 'connect_confirm.jpg'
    MAIN_TASK = 'main_task.jpg'
    NEXT_PAGE = 'next_page.jpg'

    def __init__(self, driver):
        self.CONTENT_ID = 'content'
        self.GAME_PAGE_ID = 'com.softstargames.shadow:id/unitySurfaceView'
        super().__init__(driver)

    def start_into_main(self):
        # 加速器点击加速
        # self.locator((By.XPATH,'//*[@text="Eminence"]/following-sibling::android.widget.FrameLayout')).click()
        # self.click_text('Eminence', (UiSelector.ID, 'com.excean.na:id/tv_name_middle'))
        while not self.get_match_xy(self.MAIN):

            # 跳过弹窗点击确认
            self.click_ok()
            # 断线重连
            self.click_match_xy(self.CONNECT_CLOSE)
            self.click_match_xy(self.CLOSE)
            self.click_match_xy(self.CONNECT_CONFIRM)
            if self.get_match_xy(self.DOWNLOADING):
                raise GAME_DOWNLOADING

            if self.get_match_xy(self.NEED_UPDATE):
                raise GAME_NEED_UPDATE
            try:
                self.click((By.ID, self.GAME_PAGE_ID))
                # 点击跳过
                self.click_match_xy(self.SKIP)
                # 点击关闭通知
                self.click_match_xy(self.CLOSE_CONTENT)
                # 商店弹窗取消
                self.click_cancel()

            except NoSuchElementException:
                continue
        print(ALREADY_MAIN_PAGE)
        while not self.click_main_task():
            self.click_main()
        # 通知页面
        self.click_match_xy(self.NEXT_PAGE)
        self.click_close()
        self.click_all_receive()
        self.click_close()
        self.click_next()
    def click_main(self):
        return self.click_match_xy(self.MAIN)

    def click_main_task(self):
        return self.click_match_xy(self.MAIN_TASK)

    def click_present_box(self):
        return self.click_match_xy(self.PRESENT_BOX)

    def receive_present(self):
        self.click_present_box()
        while not self.get_match_xy(self.NO_PRESENT):
            self.click_all_receive()
            self.click_ok()
            self.click_close()
        self.click_cancel()
